package com.example.demo.web;

import com.alibaba.fastjson.JSON;
import com.example.demo.mapper.firstsqlserver.GroupMemberMapper;
import com.example.demo.mapper.secondsqlserver.GetGroupMemberTotalMapper;
import com.example.demo.pojo.GroupMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class GroupMemberController {

    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Autowired
    private GetGroupMemberTotalMapper getGroupMemberTotalMapper;

    @RequestMapping("/listGroupMember")
    public String listGroupMember(@RequestParam("gId")String gId, @RequestParam("gName")String gName, Model m) {
        System.out.println(gId + " " + gName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        String startTime = today + " 00:00:00";
        String endTime = today + " 23:59:59";
        List<GroupMember> members = groupMemberMapper.listGroupMember(gId, today);
        for (int i = 0; i < members.size(); i++) {
            GroupMember member = members.get(i);
            String cid = member.getLoginName();
            int total01 = getGroupMemberTotalMapper.getTotal(cid, startTime, endTime);
            int total02 = member.getSuccess1();
            int success = member.getSuccess();
            int total = total01 + total02;
            double rate = (double)success / total;
            String str = String.format("%.3f", rate);
            double result = Double.parseDouble(str);
            member.setTotal(total);
            member.setRate(result);
        }
        String data = JSON.toJSONString(members);
        m.addAttribute("today", today);
        m.addAttribute("groupId", gId);
        m.addAttribute("groupName", gName);
        m.addAttribute("data", data);
        return "listGroupMember";
    }

    @ResponseBody
    @RequestMapping("/listGroupMemberDate")
    public List<GroupMember> listGroupMemberDate(@RequestParam("gId")String gId, @RequestParam("date")String date) {
        String startTime = date + " 00:00:00";
        String endTime = date + " 23:59:59";
        List<GroupMember> members = groupMemberMapper.listGroupMember(gId, date);
        for (int i = 0; i < members.size(); i++) {
            GroupMember member = members.get(i);
            String cid = member.getLoginName();
            int total01 = getGroupMemberTotalMapper.getTotal(cid, startTime, endTime);
            int total02 = member.getSuccess1();
            int success = member.getSuccess();
            int total = total01 + total02;
            double rate = (double)success / total;
            String str = String.format("%.3f", rate);
            double result = Double.parseDouble(str);
            member.setTotal(total);
            member.setRate(result);
        }
        return members;
    }
}
